import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';

const ns = NameSpace('project-mr');
export const RECEIVE_DATA = ns('RECEIVE_DATA');
export const RECEIVE_TOTAL_DATA = ns('RECEIVE_TOTAL_DATA');
export const SET_TAB_KEY = ns('SET_TAB_KEY');
export const SET_ORDER_KEY = ns('SET_ORDER_KEY');
export const SET_SEARCH = ns('SET_SEARCH');
export const SET_ADVANCEDSEARCHDATA = ns('SET_ADVANCEDSEARCHDATA');
export const SET_RESET_LIST = ns('SET_RESET_LIST');

const { pathResource } = window.FORCE;

export function getGroupMergeRequests(page, per_page) {
  return (dispatch, getState) => {
    const states = getState().group.groupMergeRequest;
    const { tabKey: state, search, orderKey: order_by } = states;

    const formData = {
      page,
      state,
      search,
      order_by,
      group_ids: pathResource.id,
      per_page,
    };

    dispatch(requestAndgetData(formData));
  };
}

export function getAdvancedData(page) {
  return (dispatch, getState) => {
    dispatch({
      type: SET_RESET_LIST,
    });

    const states = getState().group.groupMergeRequest;
    const { orderKey: order_by } = states;
    const search = null;

    const formData = {
      page,
      search,
      order_by,
      ...states.advancedSearchData,
    };

    dispatch(requestAndgetData(formData));
  };
}

export function requestAndgetData(formData) {
  return (dispatch) => {
    exceed
      .fetch({
        api: 'mr.advanced_search',
        data: formData,
      })
      .then((list) => {
        // 如果是加载第一页，接收数据之前清理一遍数据，防止存在之前的慢请求返回的数据
        if (formData.page === 1) {
          dispatch({
            type: SET_RESET_LIST,
          });
        }
        list.shift();
        dispatch({
          type: RECEIVE_DATA,
          data: Object.assign(list, {
            currentPage: formData.page,
          }),
        });
      })
      .fail(console.log);
    dispatch(getTotalData(formData));
  };
}

export function getTotalData(formData) {
  return (dispatch) => {
    exceed
      .fetch({
        api: 'mr.state_count',
        data: formData,
      })
      .then((list) => {
        dispatch({
          type: RECEIVE_TOTAL_DATA,
          data: list,
        });
      })
      .fail(console.log);
  };
}

export function setTabKey(data) {
  return (dispatch) => {
    dispatch({
      type: SET_TAB_KEY,
      data,
    });

    // dispatch(setResetList());
  };
}

export function setOrderKey(data) {
  return (dispatch) => {
    dispatch({
      type: SET_ORDER_KEY,
      data,
    });

    dispatch(setResetList());
  };
}

export function setSearch(data) {
  return (dispatch) => {
    dispatch({
      type: SET_SEARCH,
      data,
    });

    dispatch(setResetList());
  };
}

export function setResetList() {
  return (dispatch) => {
    dispatch({
      type: SET_RESET_LIST,
    });

    dispatch(getGroupMergeRequests(1));
  };
}

export function setAdvancedSearch(data) {
  return (dispatch) => {
    dispatch({
      type: SET_ADVANCEDSEARCHDATA,
      data,
    });

    dispatch(getAdvancedData(1));
  };
}
